#include "BlockQueue.hpp"
#include "Task.hpp"

using namespace ns_blockqueue;
using namespace ns_task;

void* consumer(void* args)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    while(1)
    {
        sleep(1);
        Task t;

        bq->Pop(&t);
        t.run();

        
        // int data = 0;
        // bq->Pop(&data);
        // cout << "消费者消耗数据： " << data << endl;
    }
}

void* producter(void* args)
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    string ops = "+-*/%";
    while(1)
    {
        sleep(1);
        int x = rand() % 20 + 1;
        int y = rand() % 10 + 1;
        char op = ops[rand() % 5];

        Task t(x, y, op);
        cout << "producter发送任务: " << x << " " << op <<  " "  << y << " = ?" << endl;
        bq->Push(t);

        
        // //1.制造数据
        // int data = rand() % 50 + 1;
        // cout << "生产者生产数据： " << data << endl;
        // bq->Push(data);
        
    }
}

int main()
{
    srand(time(0));
    BlockQueue<Task>* bq = new BlockQueue<Task>();

    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t p;

    pthread_create(&c1, nullptr, consumer, (void*)bq);
    pthread_create(&c2, nullptr, consumer, (void*)bq);
    pthread_create(&c3, nullptr, consumer, (void*)bq);
    pthread_create(&p, nullptr, producter, (void*)bq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p, nullptr);

    return 0;
}